package br.com.sovi.gwt.client.rastreamento;

import java.util.List;

import br.com.sovi.gwt.client.entity.rastreamento.Rastreado;
import br.com.sovi.gwt.client.entity.rastreamento.Rastreavel;
import br.com.sovi.gwt.client.rastreamento.service.RastreamentoService;
import br.com.sovi.gwt.client.rastreamento.service.RastreamentoServiceAsync;
import br.com.sovi.gwt.client.rpc.SecureAsyncCallback;

import com.google.gwt.core.client.GWT;
import com.gwtext.client.data.Node;
import com.gwtext.client.data.NodeTraversalCallback;
import com.gwtext.client.widgets.tree.TreeNode;
import com.gwtext.client.widgets.tree.TreePanel;
import com.gwtext.client.widgets.tree.event.TreePanelListenerAdapter;

public class RastreamentoTreePanel extends TreePanel {

	private List<? extends Rastreavel> rastreaveis = null;

	private RastreavelCheckChangeListener rastreavelCheckChangeListener;

	private TreeNode root = new TreeNode();

	private RastreamentoServiceAsync service;

	public RastreamentoTreePanel() {
		// this.setTitle("Rastreamento");
		// this.setCollapsible(true);
		// this.setIconCls("world-icon");
		this.setHeight("100%");
		this.setWidth(200);
		this.setAnimate(true);
		this.setEnableDD(true);
		this.setContainerScroll(true);
		this.setAutoScroll(true);
		this.setRootVisible(false);
		this.setFrame(true);

		this.addListener(new TreePanelListenerAdapter() {
			@Override
			public void onCheckChange(TreeNode node, final boolean checked) {
				if (node.getChildNodes().length > 0) {
					node.eachChild(new NodeTraversalCallback() {
						@Override
						public boolean execute(Node child) {
							if (child instanceof TreeNode) {
								onCheckChange((TreeNode) child, checked);
								((TreeNode) child).getUI().toggleCheck(checked);
							}
							return true;
						}
					});
				}
				else {
					if (getRastreavelCheckChangeListener() != null) {
						String text = node.getText();
						getRastreavelCheckChangeListener().onCheckChange(getRastreavelByNodeText(text), checked);
					}
				}
			}
		});

		this.setRootNode(root);

		initTree();
	}

	protected Rastreavel getRastreavelByNodeText(String text) {
		for (Rastreavel rastreavel : rastreaveis) {
			if (text.equals(rastreavel.getNome())) {
				return rastreavel;
			}
		}

		return null;
	}

	private void initTree() {
		service = (RastreamentoServiceAsync) GWT.create(RastreamentoService.class);

		service.getAllDispositivos(new SecureAsyncCallback<List<Rastreado>>() {

			@Override
			public void onSuccess(List<Rastreado> result) {
				rastreaveis = result;
				populateTree();
			}

			@Override
			public void onFailure(Throwable caught) {
				super.onFailure(caught);
				GWT.log(".onFailure() ", caught);
			}

		});
	}

	private void populateTree() {
		TreeNode semGrupoNode = new TreeNode("Sem Grupo");
		semGrupoNode.setChecked(false);
		semGrupoNode.setExpanded(true);

		if (rastreaveis != null) {
			for (Rastreavel rastreavel : rastreaveis) {
				TreeNode treeNode = new TreeNode(rastreavel.getNome());
				treeNode.setChecked(false);
				semGrupoNode.appendChild(treeNode);
			}
		}

		root.appendChild(semGrupoNode);

		this.doLayout(true);
	}

	public void setRastreavelCheckChangeListener(RastreavelCheckChangeListener rastreavelCheckChangeListener) {
		this.rastreavelCheckChangeListener = rastreavelCheckChangeListener;
	}

	public RastreavelCheckChangeListener getRastreavelCheckChangeListener() {
		return rastreavelCheckChangeListener;
	}

}
